package com.mindainfo.groundwave.web.member.web;

import com.alibaba.fastjson.JSON;
import com.mindainfo.groundwave.common.api.member.param.DeptQueryListParam;
import com.mindainfo.groundwave.common.api.member.param.DeptSaveParam;
import com.mindainfo.groundwave.common.api.member.param.DeptTodelParam;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.api.member.vo.DeptGetMyDeptsVo;
import com.mindainfo.groundwave.common.api.member.vo.DeptQueryListVo;
import com.mindainfo.groundwave.common.api.member.vo.DeptVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.DataGrid;
import com.mindainfo.groundwave.common.bean.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by MD-01 on 2017/5/16.
 */
@Controller
@RequestMapping("/sys/dept")
public class DetpController {

    @Resource
    private IDeptService deptService;

    @RequestMapping("/page")
    public String page(HttpServletRequest request){
        CommonResult<List<DeptGetMyDeptsVo>> result = deptService.getMyDepts();
        request.setAttribute("myDepts",result.getData());
        return "/member/dept";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return "/member/deptAdd";
    }

    @RequestMapping("/save")
    @ResponseBody
    public CommonResult<Void> save(Integer deptId,String name,String managerName,
                                   String managerPhone,Integer parentId){

        DeptSaveParam param = new DeptSaveParam();
        param.setDeptId(deptId);
        param.setName(name);
        param.setManagerName(managerName);
        param.setManagerPhone(managerPhone);
        param.setParentId(parentId);
        return deptService.save(param);
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public TableData<DeptQueryListVo> queryList(Integer start, Integer length, String draw,String key){

        DeptQueryListParam param = new DeptQueryListParam();
        param.setKey(key);
        param.setStart(start);
        param.setNum(length);
        CommonResult<TableData<DeptQueryListVo>> result = deptService.queryList(param);
        result.getData().setDraw(draw);
        return result.getData();
    }

    @RequestMapping("/toDel")
    @ResponseBody
    public CommonResult<Void> toDel(Integer deptId){
        DeptTodelParam param = new DeptTodelParam();
        param.setDeptId(deptId);
        return deptService.toDel(param);
    }

    @RequestMapping("/deptTree")
    @ResponseBody
    public CommonResult<List<DeptGetMyDeptsVo>> deptTree(){

        return deptService.getMyDepts();
    }
}
